using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SCHD
{
    public partial class WFArea_mant : TDV.Controles.Base.WinForm.BaseForm_Tipo01
    {
        LBArea AreaMod = new LBArea();
        private string Codigo1 = "";
        private string Codigo2 = "";
        public WFArea_mant()
        {
            InitializeComponent();
        }
        public WFArea_mant(string codigo1,string codigo2)
            :this()
        {
           Codigo1 = codigo1;
           Codigo2 = codigo2;
        }

        private void txtArea_TextChanged(object sender, EventArgs e)
        {
            if (txtArea.Text.Length > 0)
            {
                btnadicionar.Enabled = true;
            }
        }

        private void WFArea_Load(object sender, EventArgs e)
        {
            btnadicionar.Enabled = false;
            this.KeyPreview = true;
            txtArea.CharacterCasing = CharacterCasing.Upper;
            Buscar();
        }
        protected void Buscar()
        {
            LBArea area = new LBArea();
            area.Filtros.CodigoSede = Codigo1;
            area.Filtros.CodigoMArea = Codigo2;
            area.Buscar();
            dgAreas.DataSource = area;
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void btnadicionar_Click(object sender, EventArgs e)
        {
            Guardar();
        }
        protected void Guardar()
        {

            EBArea area = new EBArea();
            area.CodigoSede = Codigo1;
            area.CodigoMArea = Codigo2.ToString();
            area.DescripcionArea = txtArea.Text;
            area.Grabar();
            Buscar();
            AreaMod.Clear();
            txtArea.Text = "";
        }

        private void WFArea_mant_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Escape)
                //  MessageBox.Show("Ha presionado ESCAPAR");
                DialogResult = DialogResult.Cancel;
        }

        private void txtArea_KeyDown(object sender, KeyEventArgs e)
        {
            if (txtArea.Text.Length > 0)
            {
                btnadicionar.Enabled = true;
                if (e.KeyCode == Keys.Enter)
                {
                    Guardar();
                }
            }
        }

        private void btnAceptar_Click(object sender, EventArgs e)
        {
            foreach (EBArea _Area in AreaMod)
            {
                _Area.Grabar();
            }
            AreaMod.Clear();
            Buscar();
        }

        private void dgAreas_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (dgAreas.Columns[e.ColumnIndex].Name != null)
            {
                if (e.RowIndex > -1)
                {
                    DataGridViewRow row = dgAreas.Rows[e.RowIndex];

                    string Cod = row.Cells["Codi"].Value.ToString();
                    string Descripcion = row.Cells["cDescripcion"].Value.ToString();
                    EBArea _area = new EBArea();
                    _area.CodigoSede = Codigo1;
                    _area.CodigoMArea = Codigo2;
                    _area.CodigoArea = Cod;
                    _area.DescripcionArea = Descripcion;
                    AreaMod.Nuevo(_area);
                }

            }
        }

        private void baseButton1_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dgAreas.Rows)
            {
                DataGridViewCheckBoxCell ck1 = row.Cells[0] as DataGridViewCheckBoxCell;

                //CheckBox ck = (CheckBox)row.Cells[0].Value;
                if (Convert.ToBoolean(ck1.Value))
                {
                    try
                    {
                        EBArea area_ = new EBArea();
                        area_.CodigoSede = Codigo1;
                        area_.CodigoMArea = Codigo2.ToString();
                        area_.CodigoArea = row.Cells[1].Value.ToString(); ;
                        area_.Eliminar();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString(), "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
            }
            AreaMod.Clear();
            Buscar();
        }
    }
}

